package com.avic.service.impl;

import com.avic.common.util.AssertUtil;
import com.avic.common.util.ex.BizErrorCode;
import com.avic.common.util.ex.BizException;
import com.avic.dal.condition.PageQuerySingleSoldierCondition;
import com.avic.dal.model.SingleSoldierResource;
import com.avic.service.SingleSoldierResourceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 单兵资源service实现类
 *
 * @author: huangsheng
 * @date: 2018/9/3
 */
@Service
@Slf4j
public class SingleSoldierServiceImpl extends AbstractServiceImpl implements SingleSoldierResourceService {


    /**
     * 分页查询列表
     *
     * @see SingleSoldierResourceService#itemList(PageQuerySingleSoldierCondition)
     */
    @Override
    public List<SingleSoldierResource> itemList(PageQuerySingleSoldierCondition pageQuerySingleSoldierCondition) {

        return singleSoldierResourceMapper.itemList(pageQuerySingleSoldierCondition);
    }

    /**
     * 数据总数
     *
     * @see SingleSoldierResourceService#queryRecordsTotal(PageQuerySingleSoldierCondition)
     */
    @Override
    public int queryRecordsTotal(PageQuerySingleSoldierCondition pageQuerySingleSoldierCondition) {
        return singleSoldierResourceMapper.queryRecordsTotal(pageQuerySingleSoldierCondition);
    }

    /**
     * 新增单兵资源
     *
     * @see SingleSoldierResourceService#create(SingleSoldierResource)
     */
    @Override
    public int create(SingleSoldierResource singleSoldier) {

        AssertUtil.isTrue(singleSoldierResourceMapper.queryItemCountByPersonnelNumber(singleSoldier.getPersonnelNumber()) <= 0,
                BizErrorCode.PARAM_ERROR, "单兵资源单兵人员编号【" + singleSoldier.getPersonnelNumber() + "】已经存在");
        try {
            singleSoldierResourceMapper.insert(singleSoldier);
            log.info("创建单兵资源 {}", singleSoldier.getId());
            return singleSoldier.getId();
        } catch (DuplicateKeyException de) {
            log.warn("并发创建单兵资源", de.getMessage());
            throw new BizException(BizErrorCode.PARAM_ERROR, "单兵资源单兵编号【" + singleSoldier.getPersonnelNumber() + "】已经存在");
        } catch (Exception e) {
            log.error("创建单兵资源发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR, "创建单兵资源发生错误");
        }
    }

    /**
     * 修改单兵资源
     *
     * @see SingleSoldierResourceService#update(SingleSoldierResource)
     */
    @Override
    public void update(SingleSoldierResource singleSoldier) {
        log.info("修改单兵资源 {}", singleSoldier.getId());

        SingleSoldierResource dbSingleSoldier = singleSoldierResourceMapper.queryItemById(singleSoldier.getId());
        AssertUtil.isTrue(null != dbSingleSoldier, BizErrorCode.PARAM_ERROR, "不存在此数据");

        if (!StringUtils.equals(dbSingleSoldier.getPersonnelNumber(), singleSoldier.getPersonnelNumber())) {
            AssertUtil.isTrue(singleSoldierResourceMapper.queryItemCountByPersonnelNumber(singleSoldier.getPersonnelNumber()) <= 0,
                    BizErrorCode.PARAM_ERROR, "单兵资源单兵人员编号【" + singleSoldier.getPersonnelNumber() + "】已经存在");
        }

        try {
            singleSoldierResourceMapper.update(singleSoldier);
        } catch (DuplicateKeyException de) {
            log.warn("并发修改单兵资源", de.getMessage());
            throw new BizException(BizErrorCode.PARAM_ERROR, "单兵资源单兵编号【" + singleSoldier.getPersonnelNumber() + "】已经存在");
        } catch (Exception e) {
            log.error("修改单兵资源发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR, "修改单兵资源发生错误");
        }
    }

    /**
     * 删除单兵资源
     *
     * @see SingleSoldierResourceService#delete(int)
     */
    @Override
    public void delete(int id) {
        try {
            log.info("修改单兵资源 {}", id);
            singleSoldierResourceMapper.delete(id);
        } catch (Exception e) {
            log.error("删除单兵资源发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR, "删除单兵资源的时候发生错误");
        }
    }
}
